#!/bin/sh

# Author: Sander Vellinga
# Author: Robert Baarda

# This script is used to compile a pico program

export CLASSPATH=`pwd`/compiler/lib/aterm-java.jar:`pwd`/compiler/lib/jjtraveler.jar:`pwd`/compiler/lib/junit-4.4.jar:`pwd`/compiler/lib/shared-objects.jar:`pwd`/compiler/lib/PicoLanguage-1.0.jar:$CLASSPATH

# Check if we got an argument on the command line.
if [ -n "$1" ]; then
	echo "COMPILER: Generating pt file."
	sglr -p compiler/pico.tbl -i "$1" -o "${1:0:${#1}-5}.pt"
	echo "COMPILER: Done..."
	
	echo "COMPILER: Generating tree file."
	implodePT -t -i "${1:0:${#1}-5}.pt" -o "${1:0:${#1}-5}.tree"
	echo "COMPILER: Done..."
	
	echo "COMPILER: Compiling tree file."
	cd compiler/bin
	java nl.uva.science.luna.pico.compiler.EntryPoint ../../"${1:0:${#1}-5}.tree"
	mv stack.trm ../../"${1:0:${#1}-5}.trm"
	cd ../..
	echo "COMPILER: Done..."

	# Cleanup intermediate files
	rm "${1:0:${#1}-5}.pt"
	rm "${1:0:${#1}-5}.tree"
else
	echo "COMPILER: No .pico file specified."
	echo "COMPILER: Usage: ./compiler.sh example.pico"
fi